﻿<%@ WebHandler Language="C#" Class="KindEditor_Upload" %>

using System;
using System.Collections;
using System.Web;
using System.IO;
using System.Globalization;
using LitJson;

using Dln.Common;

public class KindEditor_Upload : IHttpHandler {

    private HttpContext context;

    public void ProcessRequest (HttpContext context) {
        this.context = context;
        string strType = this.context.Request["type"] == null ? "" : this.context.Request["type"].ToString();
        string path = "";
        string fileUrl = "";
        if (!strType.Equals("article"))
        {

            showError("上传失败！");
            return;
        }
        if (strType.Equals("article"))
        {
            //path ="E:/Test/Images/";
            //fileUrl = "http://192.168.132.104/" + "Images/";

            path ="D:/沭阳中医院工作系统/Admin/Images/";
            fileUrl = "http://192.168.11.5:8073/" + "Images/";

        }

        //定义允许上传的文件扩展名
        Hashtable extTable = new Hashtable();
        extTable.Add("image", "gif,jpg,jpeg,png,bmp");
        extTable.Add("flash", "swf,flv");
        extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
        extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
        //最大文件大小
        //int maxSize = 100000000;
        int maxSize = 3145728;

        String fileName="";//文件名
        String fileExt ="";//文件扩展名
        String dirName = "";//文件类型
        //获取上传文件类型
        dirName = context.Request.QueryString["dir"];
        if (String.IsNullOrEmpty(dirName))
        {
            dirName = "image";
        }
        if (!extTable.ContainsKey(dirName))
        {
            showError("目录名不正确。");
            return;
        }

        foreach (string upload in context.Request.Files)
        {
            if (!string.IsNullOrEmpty(upload))
            {
                //获取文件
                HttpPostedFile postedFile = context.Request.Files[upload];
                if (postedFile != null)
                {
                    //判断文件大小是否满足条件
                    if (postedFile.InputStream == null || postedFile.ContentLength > maxSize)
                    {
                        showError("上传文件大小超过限制。");
                        return;
                    }
                    //获取文件名，扩展名，文件类型
                    fileName = postedFile.FileName;
                    fileExt = Path.GetExtension(fileName).ToLower();
                    //判断文件是否满足格式
                    if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
                    {
                        showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
                        return;
                    }
                    //设置文件保存路径，返回的文件路径
                    path = path +dirName+"/";
                    fileUrl = fileUrl + dirName + "/";
                    //创建目录
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    //设置保存文件名
                    string newFileName = Guid.NewGuid().ToString() + fileExt;
                    path = path + newFileName;
                    fileUrl = fileUrl + newFileName;
                    //保存文件
                    postedFile.SaveAs(path);
                    //返回信息
                    Hashtable hash = new Hashtable();
                    hash["error"] = 0;
                    hash["url"] =  fileUrl;
                    context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
                    context.Response.Write(JsonMapper.ToJson(hash));
                    context.Response.End();
                }
            }
            showError("上传失败！找到不待上传的文件！");
        }
        HttpPostedFile imgFile = context.Request.Files["imgFile"];
        if (imgFile == null)
        {
            showError("请选择文件。");
        }
    }

    private void showError(string message)
    {
        Hashtable hash = new Hashtable();
        hash["error"] = 1;
        hash["message"] = message;
        context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
        context.Response.Write(JsonMapper.ToJson(hash));
        context.Response.End();
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}